package com.liferay.portlet.login.action;

import com.liferay.portal.CompanyMaxUsersException;
import com.liferay.portal.CookieNotSupportedException;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.PasswordExpiredException;
import com.liferay.portal.UserEmailAddressException;
import com.liferay.portal.UserIdException;
import com.liferay.portal.UserLockoutException;
import com.liferay.portal.UserPasswordException;
import com.liferay.portal.UserScreenNameException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.security.auth.AuthException;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PropsValues;
import com.liferay.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portlet.login.util.LoginUtil;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class OTPLoginAction extends LoginAction {
	private static Log _log = LogFactoryUtil.getLog(OTPLoginAction.class);

	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse) throws Exception {
		System.out.println("Inside OTPLoginAction");
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute("THEME_DISPLAY");

		if (PropsValues.AUTH_LOGIN_DISABLED) {
			actionResponse.sendRedirect(themeDisplay.getPathMain()
					+ PropsValues.AUTH_LOGIN_DISABLED_PATH);

			return;
		}

		try {
			PortletPreferences preferences = PortletPreferencesFactoryUtil
					.getPortletSetup(actionRequest);

			login(themeDisplay, actionRequest, actionResponse, preferences);

			boolean doActionAfterLogin = ParamUtil.getBoolean(actionRequest,
					"doActionAfterLogin");

			if (doActionAfterLogin)
				setForward(actionRequest, "portlet.login.login_redirect");
		} catch (Exception e) {
			if ((e instanceof AuthException)) {
				Throwable cause = e.getCause();

				if (((cause instanceof PasswordExpiredException))
						|| ((cause instanceof UserLockoutException))) {
					SessionErrors
							.add(actionRequest, cause.getClass().getName());
				} else {
					if (_log.isInfoEnabled()) {
						_log.info("Authentication failed");
					}

					SessionErrors.add(actionRequest, e.getClass().getName());
				}
			} else if (((e instanceof CompanyMaxUsersException))
					|| ((e instanceof CookieNotSupportedException))
					|| ((e instanceof NoSuchUserException))
					|| ((e instanceof PasswordExpiredException))
					|| ((e instanceof UserEmailAddressException))
					|| ((e instanceof UserIdException))
					|| ((e instanceof UserLockoutException))
					|| ((e instanceof UserPasswordException))
					|| ((e instanceof UserScreenNameException))) {
				SessionErrors.add(actionRequest, e.getClass().getName());
			} else {
				_log.error(e, e);

				PortalUtil.sendError(e, actionRequest, actionResponse);
			}
		}
	}

	protected void login(ThemeDisplay themeDisplay,
			ActionRequest actionRequest, ActionResponse actionResponse,
			PortletPreferences preferences) throws Exception {
		HttpServletRequest request = PortalUtil
				.getHttpServletRequest(actionRequest);
		HttpServletResponse response = PortalUtil
				.getHttpServletResponse(actionResponse);

		String login = ParamUtil.getString(actionRequest, "login");
		String password = actionRequest.getParameter("password");
		String otp = actionRequest.getParameter("keyOTP");
		boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");

		String authType = preferences.getValue("authType", null);

		System.out.println(otp);
		LoginUtil.login(request, response, login, "aaa", rememberMe,
				authType);

		if (PropsValues.PORTAL_JAAS_ENABLE) {
			actionResponse.sendRedirect(themeDisplay.getPathMain()
					+ "/portal/protected");
		} else {
			String redirect = ParamUtil.getString(actionRequest, "redirect");

			if (Validator.isNotNull(redirect)) {
				redirect = PortalUtil.escapeRedirect(redirect);

				if (!redirect.startsWith("http")) {
					redirect = getCompleteRedirectURL(request, redirect);
				}

				actionResponse.sendRedirect(redirect);
			} else {
				boolean doActionAfterLogin = ParamUtil.getBoolean(
						actionRequest, "doActionAfterLogin");

				if (doActionAfterLogin) {
					return;
				}

				actionResponse.sendRedirect(themeDisplay.getPathMain());
			}
		}
	}
}